﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QuanLySimSo
{
    class clsMang  //Class dinh nghia doi tuong Mang. Cac ban khong duoc chinh sua trong nay.
    {
        //string ChuoiKetNoi = @"Server=.\SQLEXPRESS;Database=QLNK_HK;Integrated Security=SSPI";
        string ChuoiKetNoi = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\sqlQLSim.mdf;Integrated Security=True;User Instance=True";

        public string m_strDauSo;
        public string m_strMang;

        public clsMang()        // khởi tạo Mạng mặc định
        {
            m_strDauSo = "";
            m_strMang = "";
        }

        public clsMang(string strDauSo)         // khởi tạo Mạng có tham số truyền vào.
        {
            m_strDauSo = strDauSo;
            m_strMang = "";
        }

        public clsMang(string strDauSo, string strMang)     //khởi tạo Mạng có tham số truyền vào.
        {
            m_strDauSo = strDauSo;
            m_strMang = strMang;
        }

        public string DAUSOSIM
        {
            get { return m_strDauSo; }
            set { this.m_strDauSo = value; }
        }

        public string NHAMANG
        {
            get { return m_strMang; }
            set { this.m_strMang = value; }
        }

        public int AddMang()    //Them mot Mang moi vao CSDL
        {
            SqlConnection Connect = new SqlConnection(ChuoiKetNoi);
            Connect.Open();
            try
            {
                string insert = "insert into Mang(DauSo,Mang) values('" + m_strDauSo + "','" + m_strMang + "')";
                SqlCommand mySqlCommand = new SqlCommand(insert, Connect);
                mySqlCommand.ExecuteNonQuery();

                Connect.Close();

                return 1; // thêm Mang thành công

            }
            catch
            {
                Connect.Close();
                return 0; // trùng Dau So
            }
        }

        public int EditMang()   //Chinh sua thong tin Mang trong CSDL
        {
            SqlConnection mySqlConnection = new SqlConnection(ChuoiKetNoi);
            SqlCommand mySqlCommand = new SqlCommand("update Mang set mang='" + m_strMang + "' where dauso ='" + m_strDauSo + "'", mySqlConnection);
            if (mySqlConnection.State == ConnectionState.Closed)
                mySqlConnection.Open();
            try
            {
                mySqlCommand.ExecuteNonQuery();
                if (mySqlConnection.State == ConnectionState.Open)
                    mySqlConnection.Close();
                return 1;
            }
            catch
            {
                mySqlConnection.Close();
                return 0;
            }
        }

        public int RemoveMang() //Xoa mot Mang ra khoi CSDL
        {
            SqlConnection mySqlConnection = new SqlConnection(ChuoiKetNoi);
            SqlCommand mySqlCommand = new SqlCommand("delete from MANG where DauSo='" + m_strDauSo + "'", mySqlConnection);
            if (mySqlConnection.State == ConnectionState.Closed)
                mySqlConnection.Open();
            try
            {
                mySqlCommand.ExecuteNonQuery();

                if (mySqlConnection.State == ConnectionState.Open)
                    mySqlConnection.Close();
                return 1;
            }
            catch
            {
                mySqlConnection.Close();
                return 0;
            }
        }

        public DataTable LayTTMang()    //Lấy thông tin Sim đưa vào DataTable để gán vào DataGridView
        {
            SqlConnection Connect = new SqlConnection(ChuoiKetNoi);
            Connect.Open();
            DataTable dt = new DataTable();
            try
            {
                string select = "select * from Mang ";
                SqlDataAdapter da = new SqlDataAdapter(select, Connect);

                da.Fill(dt);
                Connect.Close();

                return dt; // lấy thành công
            }
            catch
            {
                Connect.Close();
                return dt; // lấy không thành công
            }
        }
    }
}
